function jamsColumnSkaterCellFormat(cell, cellData, rowData, rowIndex, colIndex) {
    if (rowData.event) { // event spans the entire table
        if (colIndex == 0) {
            $(cell).attr('colspan', DXML.jamsColumns.length);
            $(cell).css('padding-top','0px');
            $(cell).css('padding-bottom','0px');
        } else {
            $(cell).hide();
        }
        return;
    }
    if (rowData.lu) { // lineup cells are each two columns wide
        if (colIndex == 0) {
        } else if (colIndex < 6) { 
            $(cell).css('background-color',"rgba(128,128,128,.25) !important");
            $(cell).attr('colspan', '2');
            $(cell).attr('height','18');
            // this does not stick around for some reason
            $(cell).css('padding-top','2px');
            $(cell).css('padding-bottom','2px');
        } else if (colIndex == 11) { // the team logo
            $(cell).attr('rowspan',2);
        } else {
            $(cell).hide();
        }
    } else { // non LU
        if (colIndex == 11) { // logo column
            $(cell).hide();
        } else if (colIndex > 1) {
//            $(cell).css('border','1px solid black');
//            if (colIndex == 10) {
//                $(cell).addClass('bordleft totalscore');
//            } else (colIndex == 9) {
//                $(cell).addClass('bordleft jamscore bordleft');
//            }
        }
    }
    if (colIndex == 0) { // handle the jam numbe which spans four rows
        if (rowData.num !== undefined) {
            $(cell).attr('rowspan',4);
            $(cell).addClass('jamnum');
//            $(cell).css('background-color','#ddd');
//            $(cell).css('border-bottom-width','1');
//            $(cell).css('border-bottom-color','white');
//            $(cell).css('border-bottom-style','solid');
//            $(cell).css('color','#333');
        } else {
            $(cell).hide();
        }
    }
}

function divForSkaterWithPenalty(skaterNum,team,penalties,prefix,postfix) {
    if (!prefix) prefix = "";
    if (!postfix) postfix = "";
    var penaltyList = penalties[skaterNum];
    if (penaltyList) {
        $(penaltyList).each(function (index, penalty) {
            var code = penalty.penalty;
            var severity = penalty.severity;
            if (!severity) severity = 'major';
            var desc = DXML.penaltyCodes[code];
            if (!desc) {
                desc = "Unknown Penalty (" + penalty + ")";
            }
            prefix += '<div class="penalty '+severity + '" + title="' + desc + '">' + code + "</div>";
        });
    }
    var skater = $(team).find("person[number=\""+skaterNum+"\"]");
    var skaterName = null;
    if (skater) {
        skaterName = $(skater).attr("name");
    }
    if (skaterName) {
        return divForSkater(skaterName,skaterNum,prefix,postfix);
    } else {
        return divForSkater(skaterNum,"",prefix,postfix);
    }
}
DXML.jamsColumns = [
    { xtitle: "#", data: function(row) {
        if (row.event) {
            return row.event;
        }
        if (row.num) {
            return row.num;
        }
        return "";
    },
    },
    { xtitle: "Events", data: function(row) { 
        if (row.event) { return ""; }
        if (row.lu) {
            return divForSkaterWithPenalty(row.lu.jammer,row.team,row.penalties, DXML.jamJammerMarker);
        }
        var retval = "";
        if (JamLost(row.jam, row.team)) retval += DXML.jamLostMarker;
        if (JamLead(row.jam, row.team)) retval += DXML.jamLeadMarker;
        if (JamCalled(row.jam, row.team)) retval += DXML.jamCalledMarker;
        if (JamNP(row.jam, row.team)) retval += DXML.jamNPMarker;
        if (row.starPass == 1) retval += DXML.jamStarPassMarker;
        return retval;
        },
        class:"bordleft",
    },
    { xtitle: "Pass2", data: function(row) { 
        if (row.event) { return ""; }
        if (row.lu) {
            if (row.lu.pivot) {
                return divForSkaterWithPenalty(row.lu.pivot, row.team, row.penalties, DXML.jamPivotMarker);
            } else {
                return divForSkaterWithPenalty(row.lu.blockers[3], row.team, row.penalties, DXML.jamNoPivotMarker);
            }
        }
        return formatJamPassScore(JamScoringPasses(row.jam, row.team), 2, row.starPass == 2); 
    },
    },
    { xtitle: "Pass3", data: function(row) { 
        if (row.event) { return ""; }
        if (row.lu) {
            return divForSkaterWithPenalty(row.lu.blockers[0], row.team, row.penalties, DXML.jamBlockerMarker);
        }
        return formatJamPassScore(JamScoringPasses(row.jam, row.team), 3, row.starPass == 3); 
    },
    },
    { xtitle: "Pass4", data: function(row) { 
        if (row.event) { return ""; }
        if (row.lu) {
            return divForSkaterWithPenalty(row.lu.blockers[1], row.team, row.penalties,DXML.jamBlockerMarker);
        }
        return formatJamPassScore(JamScoringPasses(row.jam, row.team), 4, row.starPass == 4); 
    },
    },
    { xtitle: "Pass5", data: function(row) { 
        if (row.event) { return ""; }
        if (row.lu) {
            return divForSkaterWithPenalty(row.lu.blockers[2], row.team, row.penalties,DXML.jamBlockerMarker);
        }
        return formatJamPassScore(JamScoringPasses(row.jam, row.team), 5, row.starPass == 5); },
     },
    { xtitle: "Pass6", data: function(row) { 
        if (row.event) { return ""; }
        if (row.lu) { return ""; }
        return formatJamPassScore(JamScoringPasses(row.jam, row.team), 6, row.starPass == 6); },
    },
    { xtitle: "Pass7", data: function(row) { 
        if (row.event) { return ""; }
        if (row.lu) { return ""; }
        return formatJamPassScore(JamScoringPasses(row.jam, row.team), 7, row.starPass == 7); },
     },
    { xtitle: "Pass8", data: function(row) { 
        if (row.event) { return ""; }
        if (row.lu) { return ""; }
        return formatJamPassScore(JamScoringPasses(row.jam, row.team), 8, row.starPass == 8); },
     },
    
    { xtitle: "Jam Total", data: function(row) { 
        if (row.event) { return ""; }
        if (row.lu) { return ""; }
        return JamPoints(row.jam, row.team); },
        class: 'bordleft jamscore'
     },
    { xtitle: "Total", data: function(row) {
        if (row.event) { return ""; }
        if (row.lu) { return ""; }
        return row.total; },
        class: 'bordleft totalscore'
     },
     { xtitle: "Logo", data: function(row) {
            if (row.event) { return ""; }
            if (row.lu) {  
                if (row.team == DXML.homeTeam) {
                    return "<div class=\"teamicon homeSmallIcon\"/>";
                } else {
                    return "<div class=\"teamicon awaySmallIcon\"/>";
                }
            }
            return "";
         },
     class: 'bordleft totalscore',
     width: 32
     }
]
function prepareJamsPage() {
    if (!DXML.firstPeriod) {
        return;
    }
    var lines = [];
    var homeTotal = 0;
    var awayTotal = 0;
    var p1jams = $(DXML.firstPeriod).find("jam").length;
    lines.push({event:"Period One", class:"period", height:28});
    var oddJam = false;
    $([DXML.firstPeriod,DXML.secondPeriod]).find("jam").each(function (index, jam) {
        homeTotal += JamPoints(jam, DXML.homeTeam);
        awayTotal += JamPoints(jam, DXML.awayTeam);
        var jamNum = index+1;
        if (jamNum == p1jams + 1) {
            lines.push({event:"Period Two", class:"period", height:28});
        }
        if (jamNum > p1jams) {
            jamNum -= p1jams;
        }
        var homePenalties = {};
        var awayPenalties = {};
        $(jam).find("penalty").each(function (index, penalty) {
            var penalties = {};
            if (EventIsTeam(penalty, DXML.homeTeam)) {
                penalties = homePenalties;
            } else if (EventIsTeam(penalty, DXML.awayTeam)) {
                penalties = awayPenalties;
            }
            var skater = $(penalty).attr("skater");
            if (!penalties[skater]) penalties[skater] = [];
            penalties[skater].push({ 
                penalty:$(penalty).attr("penalty"),
                severity:$(penalty).attr("severity")
            });
        });
        lines.push({oddJam: oddJam, num: jamNum, jam:jam, team:DXML.homeTeam, lu:PositionsInJam(jam, DXML.homeTeam), penalties:homePenalties});
        lines.push({oddJam: oddJam, jam:jam, starPass: JamStarPassPass(jam, DXML.homeTeam), team:DXML.homeTeam, total: homeTotal});
//        if (JamStarPass(jam, DXML.homeTeam))
//            lines.push({oddJam: oddJam, jam:jam, starPass: true, team:DXML.homeTeam, total: homeTotal});
        lines.push({oddJam: oddJam, jam:jam, team:DXML.awayTeam, lu:PositionsInJam(jam, DXML.awayTeam), penalties:awayPenalties});
        lines.push({oddJam: oddJam, jam:jam, starPass: JamStarPassPass(jam, DXML.awayTeam), team:DXML.awayTeam, total: awayTotal});
//        if (JamStarPass(jam, DXML.awayTeam))
//            lines.push({oddJam: oddJam, jam:jam, starPass: true, team:DXML.awayTeam, total: awayTotal});

        var hasDivide = false;
        if (JamInj(jam)) {
            lines.push({event:"Injury", color:"red", height:28});
            hasDivide = true;
        }
        // check for time out
        if (!hasDivide) {
            lines.push({event:" ",color:"#222",height:"1",padding:"0"}); // add divider
        }
        oddJam = !oddJam;
    });
    $(DXML.jamsColumns).each(function(index, value) {
        value.createdCell = jamsColumnSkaterCellFormat;
    });
    var tableJ = $("#jamsTable").DataTable({
        paging: false,
        searching: false,
        ordering: false,
        info: false,
        data: lines,
        columns: DXML.jamsColumns,
        rowCallback: function( row, data ) {
            if (data.oddJam) {
                $(row).addClass('oddJam');
            }
            if (data.event) {
                $(row).addClass('home');
                if (data.class) {
                    $(row).addClass(data.class);
                }
                if (data.height) {
                    $(row).css("height",data.height);
                }
                if (data.color)
                    $(row).css("background-color",data.color);
                else
                    $(row).css("background-color","gray");
            } else if (data.team == DXML.homeTeam) {
                $(row).attr("jam");
                $(row).addClass('home');
//                $(row).css("color","red");
                $(row).css("border-bottom","1px solid #888");
            } else if (data.team == DXML.awayTeam) {
                $(row).attr("jam");
                $(row).addClass('visitor');
//                $(row).css("color","blue");
                $(row).css("border-bottom","1px solid #888");
            }
        }
    });
    $("#jamsTable").css("width","");
    // Highlight player throughout table
    addPlayerHighlighting("#jamsTable");
    // since the layout screws up the padding, do it now
    $('#jamsTable .player').parent().css('padding-top',"3px").css('padding-bottom',"3px");
    
    $("#jamsTable .oddJam").css('background-color','rgb(212, 219, 225)');
    $("#jamsTable .oddJam .jamnum").css('background-color','rgb(212, 219, 225)');
   //    new $.fn.dataTable.FixedHeader( tableJ );
}

